<template>
  <div>
    <el-card>
      <div id="main" style="width: 800px;height:400px;"></div>
    </el-card>
  </div>
</template>

<script>
import { apiGetReports } from '@/api/index.js'
import echarts from 'echarts'
import _ from 'lodash'

export default {
  name: 'Report',
  mounted () {
    this.initCharts()
  },
  data () {
    return {
      options: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }
  },
  methods: {
    async initCharts () {
      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(document.getElementById('main'))

      const { data: res } = await apiGetReports()
      if (res.meta.status !== 200) {
        return this.$message.error(`${res.meta.status}: ${res.meta.msg}`)
      }
      const result = _.merge(res.data, this.options)
      // 绘制图表
      myChart.setOption(result)
    }
  }
}
</script>

<style scoped>

</style>
